//
//  DetailDepartmentViewController.m
//  棒棒哒
//
//  Created by rimi on 15/8/7.
//  Copyright (c) 2015年 rimi. All rights reserved.
//

#import "DetailDepartmentViewController.h"
#import "Config.h"
#import "DetailDiseaseScrollView.h"
#import "UIImageView+WebCache.h"
@interface DetailDepartmentViewController ()

@property (nonatomic, strong)NSMutableDictionary *dataSource;
@property (nonatomic ,assign)CGFloat                height;
@property (nonatomic, strong)DetailDiseaseScrollView *scrollView;



@end

@implementation DetailDepartmentViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    [self initializeUserInterface];
    [self networkRequest];
    
}

-(void)initializeDataSource
{
    for (NSString *key in [_dataSource allKeys]) {
        if ([_dataSource[key]isKindOfClass:[NSString class]]) {
            NSString *string = _dataSource[key];
            string = [string stringByReplacingOccurrencesOfString:@"<div class=\"moduleContent\">" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"</p>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"<p>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"<br />" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"</div>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"</strong>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"<strong>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"</a>" withString:@""];
            string = [string stringByReplacingOccurrencesOfString:@"&nbsp;" withString:@""];
            string = [string stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
            
            
            _dataSource[key] = string;
            
            
            
            
          
        }
        
        
    }
    
}

-(void)initializeUserInterface
{
    self.view.backgroundColor = [UIColor whiteColor];

}

-(void)loadAfterNetwork
{

        [self initializeDataSource];
        //初始化一个高度记录各label高度，最后计算scrollview的contentview高度
        _height = 0 ;
        _scrollView = [DetailDiseaseScrollView instanceAddContactView];
        _scrollView.frame = CGRectMake(0, NAV_HEIGHT, SCREEN_WIDTH, SCREEN_HEIGHT - NAV_HEIGHT);
        
    
        //疾病名称
        _scrollView.nameLabel.text = _dataSource[@"name"];
    //疾病图片
        NSString *urlString = [@"http://www.yi18.net/" stringByAppendingString:_dataSource[@"img"]];
        [_scrollView.imageVIew sd_setImageWithURL:[NSURL URLWithString:urlString]];
    //科室
        _scrollView.departmentLabel.text =[@"就诊科室：" stringByAppendingString: _dataSource[@"department"]];
    
        _scrollView.partLabel.text =[@"身体部位：" stringByAppendingString:  _dataSource[@"place"]];
    //简介
        UILabel *summary = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _scrollView.imageVIew.frame.origin.y + _scrollView.imageVIew.bounds.size.height + 20)  Text:@"疾病简介：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:summary];
    
        _scrollView.summaryLabel = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height+ 10) Text:_dataSource[@"summary"] FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
            }];
        //病状详情
        UILabel *symptomText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height +10)  Text:@"病状详情：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:symptomText];
        _scrollView.symptomText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 10) Text:_dataSource[@"symptomText"] FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
    //食疗
    UILabel *foodText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height +10)  Text:@"食疗：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
        _height = finalY;
    }];
    [_scrollView addSubview:foodText];
        _scrollView.foodText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 10) Text:_dataSource[@"foodText"] FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        //相关疾病
        UILabel *disease = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height +10)  Text:@"相关疾病：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:disease];
    
        _scrollView.disease = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 10) Text:_dataSource[@"disease"] FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        //药物详情
        UILabel *drugText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height +10)  Text:@"相关疾病：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:drugText];

        _scrollView.drugText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 10) Text:_dataSource[@"drugText"] FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        //病因
        UILabel *causeText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x,_height +10)  Text:@"病因：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:causeText];
            _scrollView.causeText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 20) Text:_dataSource[@"causeText"]  FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
                _height = finalY;
            }];
        //治疗方案
        UILabel *careText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.imageVIew.frame.origin.x, _height +10)  Text:@"治疗方案：" FontSize:16 TextAlignment:0 TextColor:[UIColor blackColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        [_scrollView addSubview:careText];
        _scrollView.careText = [CustomLabel labelWithOrigin:CGPointMake(_scrollView.summaryLabel.frame.origin.x, _height + 20) Text:_dataSource[@"careText"]  FontSize:14 TextAlignment:0 TextColor:[UIColor darkGrayColor] BackGroundColor:nil CornerRadius:0 BorderWidth:0 BorderColor:nil finalY:^(CGFloat finalY) {
            _height = finalY;
        }];
        
            _scrollView.contentSize = CGSizeMake(SCREEN_WIDTH, _height +10);
            [_scrollView addSubviews];
            [self.view addSubview:_scrollView];

}

-(void)loadWhenLoadFailWithUnKnownError
{
    UIButton *retry = [CustomButton buttonWithFrame:CGRectZero Title:@"\n数据加载失败\n   点击重试" BackGroudImage:nil BackGroundColor:[UIColor orangeColor] TextColor:[UIColor blackColor] CornerRadius:50 BorderWidth:0 BorderColor:nil];
    retry.titleLabel.font = [UIFont systemFontOfSize:14];
    retry.titleLabel.numberOfLines = 3;
    retry.bounds = CGRectMake(0, 0, 100, 100);
    retry.transform = CGAffineTransformMakeScale(0.001, 0.001);
    retry.center = self.view.center;
    [retry addTarget:self action:@selector(handleButtonReTry:) forControlEvents:64];
    [UIView animateWithDuration:1 delay:0 usingSpringWithDamping:0.4 initialSpringVelocity:0.1 options:UIViewAnimationOptionTransitionFlipFromLeft animations:^{
        retry.transform = CGAffineTransformMakeScale(1.0, 1.0);
    } completion:nil];
    [self.view addSubview:retry];
}
-(void)handleButtonReTry:(UIButton *)sender
{
    
    [UIView animateWithDuration:0.5 animations:^{
        sender.transform = CGAffineTransformMakeScale(0.001, 0.001);
    } completion:^(BOOL finished) {
        [sender removeFromSuperview];
        [self networkRequest];
        
    }];
}


-(void)networkRequest
{

    NetRequest *request = [[NetRequest alloc]init];
    [request request:@"http://apis.baidu.com/yi18/disease/detail" withHttpArg:[NSString stringWithFormat:@"id=%@",_departmentId] success:^(id responseObject) {
        NSLog(@"!!!!%@",responseObject);
        _dataSource = [responseObject[@"yi18"]mutableCopy];
        @try {
            [self loadAfterNetwork];
        }
        @catch (NSException *exception) {
            [self loadWhenLoadFailWithUnKnownError];
        }
        @finally {
            
        }
    } failure:^(NSError *error) {
        
    }];
}



@end
